package com.mrzhou.core.tool.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * jaspty 配置加密工具类
 * @author qiang.zhou
 */
public class JasyptUtil {

    /**
     * 加密数据
     * @param plainText 需要加密的原始报文
     * @param factor 密码串
     * @return
     */
    public static String encryptWithSHA512(String plainText, String factor) {
        // 1. 创建解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        // 为减少配置文件的书写，以下都是 Jasypt 3.x 版本，配置文件默认配置
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        // 3. 加密
        return encryptor.encrypt(plainText);
    }

    public static void main(String[] args) {
        System.out.println("ENC(" + JasyptUtil.encryptWithSHA512("mysql", "mrzhou-cloud") + ")");
    }


}
